% Macros for The TeXbook

\catcode`@=11 % borrow the private macros of PLAIN (with care)

\font\tentex=cmtex10

\font\inchhigh=cminch
\font\titlefont=cmssdc10 at 40pt

\font\ninerm=cmr9
\font\eightrm=cmr8
\font\sixrm=cmr6

\font\ninei=cmmi9
\font\eighti=cmmi8
\font\sixi=cmmi6
\skewchar\ninei='177 \skewchar\eighti='177 \skewchar\sixi='177

\font\ninesy=cmsy9
\font\eightsy=cmsy8
\font\sixsy=cmsy6
\skewchar\ninesy='60 \skewchar\eightsy='60 \skewchar\sixsy='60

\font\eightss=cmssq8

\font\eightssi=cmssqi8

\font\ninebf=cmbx9
\font\eightbf=cmbx8
\font\sixbf=cmbx6

\font\ninett=cmtt9
\font\eighttt=cmtt8

\hyphenchar\tentt=-1 % inhibit hyphenation in typewriter type
\hyphenchar\ninett=-1
\hyphenchar\eighttt=-1

\font\ninesl=cmsl9
\font\eightsl=cmsl8

\font\nineit=cmti9
\font\eightit=cmti8

\font\tenu=cmu10 % unslanted text italic
\font\magnifiedfiverm=cmr5 at 10pt
\font\manual=manfnt % font used for the METAFONT logo, etc.
\font\cmman=cmman % font used for miscellaneous Computer Modern variations

\newskip\ttglue
\def\tenpoint{\def\rm{\fam0\tenrm}%
  \textfont0=\tenrm \scriptfont0=\sevenrm \scriptscriptfont0=\fiverm
  \textfont1=\teni \scriptfont1=\seveni \scriptscriptfont1=\fivei
  \textfont2=\tensy \scriptfont2=\sevensy \scriptscriptfont2=\fivesy
  \textfont3=\tenex \scriptfont3=\tenex \scriptscriptfont3=\tenex
  \def\it{\fam\itfam\tenit}%
  \textfont\itfam=\tenit
  \def\sl{\fam\slfam\tensl}%
  \textfont\slfam=\tensl
  \def\bf{\fam\bffam\tenbf}%
  \textfont\bffam=\tenbf \scriptfont\bffam=\sevenbf
   \scriptscriptfont\bffam=\fivebf
  \def\tt{\fam\ttfam\tentt}%
  \textfont\ttfam=\tentt
  \tt \ttglue=.5em plus.25em minus.15em
  \normalbaselineskip=12pt
  \def\MF{{\manual META}\-{\manual FONT}}%
  \let\sc=\eightrm
  \let\big=\tenbig
  \setbox\strutbox=\hbox{\vrule height8.5pt depth3.5pt width\z@}%
  \normalbaselines\rm}

\def\ninepoint{\def\rm{\fam0\ninerm}%
  \textfont0=\ninerm \scriptfont0=\sixrm \scriptscriptfont0=\fiverm
  \textfont1=\ninei \scriptfont1=\sixi \scriptscriptfont1=\fivei
  \textfont2=\ninesy \scriptfont2=\sixsy \scriptscriptfont2=\fivesy
  \textfont3=\tenex \scriptfont3=\tenex \scriptscriptfont3=\tenex
  \def\it{\fam\itfam\nineit}%
  \textfont\itfam=\nineit
  \def\sl{\fam\slfam\ninesl}%
  \textfont\slfam=\ninesl
  \def\bf{\fam\bffam\ninebf}%
  \textfont\bffam=\ninebf \scriptfont\bffam=\sixbf
   \scriptscriptfont\bffam=\fivebf
  \def\tt{\fam\ttfam\ninett}%
  \textfont\ttfam=\ninett
  \tt \ttglue=.5em plus.25em minus.15em
  \normalbaselineskip=11pt
  \def\MF{{\manual hijk}\-{\manual lmnj}}%
  \let\sc=\sevenrm
  \let\big=\ninebig
  \setbox\strutbox=\hbox{\vrule height8pt depth3pt width\z@}%
  \normalbaselines\rm}

\def\eightpoint{\def\rm{\fam0\eightrm}%
  \textfont0=\eightrm \scriptfont0=\sixrm \scriptscriptfont0=\fiverm
  \textfont1=\eighti \scriptfont1=\sixi \scriptscriptfont1=\fivei
  \textfont2=\eightsy \scriptfont2=\sixsy \scriptscriptfont2=\fivesy
  \textfont3=\tenex \scriptfont3=\tenex \scriptscriptfont3=\tenex
  \def\it{\fam\itfam\eightit}%
  \textfont\itfam=\eightit
  \def\sl{\fam\slfam\eightsl}%
  \textfont\slfam=\eightsl
  \def\bf{\fam\bffam\eightbf}%
  \textfont\bffam=\eightbf \scriptfont\bffam=\sixbf
   \scriptscriptfont\bffam=\fivebf
  \def\tt{\fam\ttfam\eighttt}%
  \textfont\ttfam=\eighttt
  \tt \ttglue=.5em plus.25em minus.15em
  \normalbaselineskip=9pt
  \def\MF{{\manual opqr}\-{\manual stuq}}%
  \let\sc=\sixrm
  \let\big=\eightbig
  \setbox\strutbox=\hbox{\vrule height7pt depth2pt width\z@}%
  \normalbaselines\rm}

\def\tenmath{\tenpoint\fam-1 } % use after $ in ninepoint sections
\def\tenbig#1{{\hbox{$\left#1\vbox to8.5pt{}\right.\n@space$}}}
\def\ninebig#1{{\hbox{$\textfont0=\tenrm\textfont2=\tensy
  \left#1\vbox to7.25pt{}\right.\n@space$}}}
\def\eightbig#1{{\hbox{$\textfont0=\ninerm\textfont2=\ninesy
  \left#1\vbox to6.5pt{}\right.\n@space$}}}

% Page layout
\newdimen\pagewidth \newdimen\pageheight \newdimen\ruleht
\hsize=29pc  \vsize=44pc  \maxdepth=2.2pt  \parindent=3pc
\pagewidth=\hsize \pageheight=\vsize \ruleht=.5pt
\abovedisplayskip=6pt plus 3pt minus 1pt
\belowdisplayskip=6pt plus 3pt minus 1pt
\abovedisplayshortskip=0pt plus 3pt
\belowdisplayshortskip=4pt plus 3pt

%\newinsert\footins
\def\footnote#1{\edef\@sf{\spacefactor\the\spacefactor}#1\@sf
      \insert\footins\bgroup\eightpoint
      \interlinepenalty100 \let\par=\endgraf
        \leftskip=\z@skip \rightskip=\z@skip
        \splittopskip=10pt plus 1pt minus 1pt \floatingpenalty=20000
        \smallskip\item{#1}\bgroup\strut\aftergroup\@foot\let\next}
\skip\footins=12pt plus 2pt minus 4pt % space added when footnote is present
%\count\footins=1000 % footnote magnification factor (1 to 1)
\dimen\footins=30pc % maximum footnotes per page

\newinsert\margin
\dimen\margin=\maxdimen
%\count\margin=0 \skip\margin=0pt % marginal inserts take up no space

\newif\iftitle
\def\titlepage{\global\titletrue} % for pages without headlines
\def\rhead{} % \rhead contains the running headline

\def\leftheadline{\hbox to \pagewidth{%
    \vbox to 10pt{}% strut to position the baseline
    \llap{\tenbf\folio\kern1pc}% folio to left of text
    \tenit\rhead\hfil% running head flush left
    }}
\def\rightheadline{\hbox to \pagewidth{%
    \vbox to 10pt{}% strut to position the baseline
    \hfil\tenit\rhead\/% running head flush right
    \rlap{\kern1pc\tenbf\folio}% folio to right of text
    }}

\def\onepageout#1{\shipout\vbox{ % here we define one page of output
    \offinterlineskip % butt the boxes together
    \vbox to 3pc{ % this part goes on top of the 44pc pages
      \iftitle % the next is used for title pages
        \global\titlefalse % reset the titlepage switch
        \setcornerrules % for camera alignment
      \else\ifodd\pageno \rightheadline\else\leftheadline\fi\fi
      \vfill} % this completes the \vbox to 3pc
    \vbox to \pageheight{
      \ifvoid\margin\else % marginal info is present
        \rlap{\kern31pc\vbox to\z@{\kern4pt\box\margin \vss}}\fi
      #1 % now insert the main information
      \ifvoid\footins\else % footnote info is present
        \vskip\skip\footins \kern-3pt
        \hrule height\ruleht width\pagewidth \kern-\ruleht \kern3pt
        \unvbox\footins\fi
      \boxmaxdepth=\maxdepth
      } % this completes the \vbox to \pageheight
    }
  \advancepageno}

\def\setcornerrules{\hbox to \pagewidth{\vrule width 1pc height\ruleht
    \hfil \vrule width 1pc}
  \hbox to \pagewidth{\llap{\sevenrm(page \folio)\kern1pc}%
    \vrule height1pc width\ruleht depth\z@
    \hfil \vrule width\ruleht depth\z@}}

\output{\onepageout{\unvbox255}}

\newbox\partialpage
\def\begindoublecolumns{\begingroup
  \output={\global\setbox\partialpage=\vbox{\unvbox255\bigskip}}\eject
  \output={\doublecolumnout} \hsize=14pc \vsize=89pc}
\def\enddoublecolumns{\output={\balancecolumns}\eject
  \endgroup \pagegoal=\vsize}

\def\doublecolumnout{\splittopskip=\topskip \splitmaxdepth=\maxdepth
  \dimen@=44pc \advance\dimen@ by-\ht\partialpage
  \setbox0=\vsplit255 to\dimen@ \setbox2=\vsplit255 to\dimen@
  \onepageout\pagesofar
  \unvbox255 \penalty\outputpenalty}
\def\pagesofar{\unvbox\partialpage
  \wd0=\hsize \wd2=\hsize \hbox to\pagewidth{\box0\hfil\box2}}
\def\balancecolumns{\setbox0=\vbox{\unvbox255} \dimen@=\ht0
  \advance\dimen@ by\topskip \advance\dimen@ by-\baselineskip
  \divide\dimen@ by2 \splittopskip=\topskip
  {\vbadness=10000 \loop \global\setbox3=\copy0
    \global\setbox1=\vsplit3 to\dimen@
    \ifdim\ht3>\dimen@ \global\advance\dimen@ by1pt \repeat}
  \setbox0=\vbox to\dimen@{\unvbox1}
  \setbox2=\vbox to\dimen@{\unvbox3}
  \pagesofar}

% To produce only a subset of pages, put the page numbers on separate
% lines in a file called pages.tex
\let\Shipout=\shipout
\newread\pages \newcount\nextpage \openin\pages=pages
\def\getnextpage{\ifeof\pages\else
 {\endlinechar=-1\read\pages to\next
  \ifx\next\empty % in this case we should have eof now
  \else\global\nextpage=\next\fi}\fi}
\ifeof\pages\else\message{OK, I'll ship only the requested pages!}
 \getnextpage\fi
\def\shipout{\ifeof\pages\let\next=\Shipout
 \else\ifnum\pageno=\nextpage\getnextpage\let\next=\Shipout
  \else\let\next=\Tosspage\fi\fi \next}
\newbox\garbage \def\Tosspage{\deadcycles=0\setbox\garbage=}

% Chapter formatting
% The preface and table of contents are formatted in place, not here

\newcount\exno % for the number of exercises in the current chapter
\newcount\subsecno % for the number of subsections in the current chapter

\def\beginchapter#1 #2#3. #4\par{\global\exno=0
  \subsecno=0
  \def\chapno{#2#3}
  \ifodd\pageno
    \errmessage{You had too much text on that last page; I'm backing up}
    \advance\pageno by-1 \fi
  \titlepage
  \def\\{ } % \\'s in the title will be treated as spaces
  \message{#1 #2#3:} % show the chapter title on the terminal
  \def\MF{{\manual 89:;<=>:}} % slant the logo
  \xdef\rhead{#1 #2#3: #4\unskip}
  {\def\TeX{T\kern-.2em\lower.5ex\hbox{E}\kern-.06em X}
    \def\MF{{\vbox to30pt{}\manual ()*+,-.*}}
    \def\\{#3}
    \ifx\empty\\ \rightline{\inchhigh #2\kern-.04em}
    \else\rightline{\inchhigh #2\kern-.06em#3\kern-.04em}\fi
    \vskip 1.75pc
    \baselineskip 36pt \lineskiplimit \titlelsl \lineskip 12pt
    \let\\=\cr % now the \\'s are line dividers
    \halign{\line{\titlefont\hfil##}\\#4\unskip\\}
    \vfill\eject} % output the chapter title page
  \tenpoint
  \noindent\ignorespaces} % the first paragraph of a chapter is not indented
\newdimen\titlelsl \titlelsl=1pt

\outer\def\endchapter{\ifodd\pageno \else\vfill\eject\null\fi
  \begingroup\bigskip\vfill % beginning of the quotes
  \def\eject{\endgroup\eject}
  \def\par{\ifhmode\/\endgraf\fi}\obeylines
  \def\TeX{T\kern-.2em\lower.5ex\hbox{E}\kern-.000em X}
  \def\MF{{\manual opqr}\-{\manual stuq}}
  \eightpoint \let\tt=\ninett
  \baselineskip 10pt
  \parfillskip \z@
  \interlinepenalty 10000
  \leftskip \z@ plus 40pc minus \parindent
  \let\rm=\eightss \let\sl=\eightssi
  \everypar{\sl}}
\def\author#1(#2){\smallskip\noindent\rm--- #1\unskip\enspace(#2)}

\def\dbend{{\manual\char127}} % dangerous bend sign
\def\d@nger{\medbreak\begingroup\clubpenalty=10000
  \def\par{\endgraf\endgroup\medbreak} \noindent\hang\hangafter=-2
  \hbox to0pt{\hskip-\hangindent\dbend\hfill}\ninepoint}
\outer\def\danger{\d@nger}
\def\dd@nger{\medbreak\begingroup\clubpenalty=10000
  \def\par{\endgraf\endgroup\medbreak} \noindent\hang\hangafter=-2
  \hbox to0pt{\hskip-\hangindent\dbend\kern1pt\dbend\hfill}\ninepoint}
\outer\def\ddanger{\dd@nger}
\def\enddanger{\endgraf\endgroup} % omits the \medbreak

\outer\def\subsection#1. {\medbreak\advance\subsecno by 1
  \noindent{\it \the\subsecno.\enspace#1.\enspace}}
\def\ansno#1.#2:{\medbreak\noindent
  \hbox to\parindent{\bf\hss#1.#2.\enspace}\ignorespaces}

% Composition macros
\hyphenation{man-u-script man-u-scripts ap-pen-dix xscaled}

\def\AmSTeX{$\cal A\kern-.1667em\lower.5ex\hbox{$\cal M$}\kern-.075em
  S$-\TeX}
\def\bull{\vrule height .9ex width .8ex depth -.1ex } % square bullet
\def\SS{{\it SS}} % scriptscript style
\def\|{\leavevmode\hbox{\tt\char`\|}} % vertical line
\def\dn{\leavevmode\hbox{\tt\char'14}} % downward arrow
\def\up{\leavevmode\hbox{\tt\char'13}} % upward arrow
\def\]{\leavevmode\hbox{\tt\char`\ }} % visible space

\def\pt{\,{\rm pt}} % units of points, in math formulas
\def\em{\,{\rm em}} % units of ems, in math formulas
\def\<#1>{\leavevmode\hbox{$\langle$#1\/$\rangle$}} % syntactic quantity
\def\oct#1{\hbox{\rm\'{}\kern-.2em\it#1\/\kern.05em}} % octal constant
\def\hex#1{\hbox{\rm\H{}\tt#1}} % hexadecimal constant
\def\cstok#1{\leavevmode\thinspace\hbox{\vrule\vtop{\vbox{\hrule\kern1pt
        \hbox{\vphantom{\tt/}\thinspace{\tt#1}\thinspace}}
      \kern1pt\hrule}\vrule}\thinspace} % control sequence token

{\obeyspaces\gdef {\ }}
\def\parbreak{\hfil\break\indent\strut}
\def\stretch{\nobreak\hskip0pt plus2pt\relax}

% macros for non-centered displays
\outer\def\begindisplay{\obeylines\startdisplay}
{\obeylines\gdef\startdisplay#1
  {\catcode`\^^M=5$$#1\halign\bgroup\indent##\hfil&&\qquad##\hfil\cr}}
\outer\def\enddisplay{\crcr\egroup$$}

% (the following \begin...\end-type macros do not appear in Appendix E)
% macros for demonstrating math constructions
\outer\def\beginmathdemo{$$\advance\baselineskip by2pt
  \halign\bgroup\indent\hbox to 160pt{##\hfil}&$##$\hfil\cr\noalign{\vskip-2pt}}
\outer\def\begindisplaymathdemo {$$\advance\baselineskip by15pt
  \halign\bgroup\indent\hbox to 160pt{##\hfil}&$\displaystyle{##}$\hfil\cr
  \noalign{\vskip-15pt}}
\outer\def\beginlongmathdemo{$$\advance\baselineskip by2pt
  \halign\bgroup\indent\hbox to 210pt{##\hfil}&$##$\hfil\cr\noalign{\vskip-2pt}}
\outer\def\beginlongdisplaymathdemo {$$\advance\baselineskip by15pt
  \halign\bgroup\indent\hbox to 210pt{##\hfil}&$\displaystyle{##}$\hfil\cr
  \noalign{\vskip-15pt}}
\outer\def\endmathdemo{\egroup$$}

% macros for font tables
\def\oddline#1{\cr
  \noalign{\nointerlineskip}
  \multispan{19}\hrulefill&
  \setbox0=\hbox{\lower 2.3pt\hbox{\hex{#1x}}}\smash{\box0}\cr
  \noalign{\nointerlineskip}}
\def\evenline{\cr\noalign{\hrule}}
\def\chartstrut{\lower4.5pt\vbox to14pt{}}
\def\beginchart#1{$$\postdisplaypenalty=-10000 \global\count@=0 #1
  \halign to\hsize\bgroup
    \chartstrut##\tabskip0pt plus10pt&
    &\hfil##\hfil&\vrule##\cr
    \lower6.5pt\null
    &&&\oct0&&\oct1&&\oct2&&\oct3&&\oct4&&\oct5&&\oct6&&\oct7&\evenline}
\def\endchart{\raise11.5pt\null&&&\hex 8&&\hex 9&&\hex A&&\hex B&
  &\hex C&&\hex D&&\hex E&&\hex F&\cr\egroup$$}
\def\:{\setbox0=\hbox{\char\count@}%
  \ifdim\ht0>7.5pt\reposition
  \else\ifdim\dp0>2.5pt\reposition\fi\fi
  \box0\global\advance\count@ by1 }
\def\reposition{\setbox0=\hbox{$\vcenter{\kern2pt\box0\kern2pt}$}}
\def\normalchart{%
  &\oct{00x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\oddline0
  &\oct{01x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&\evenline
  &\oct{02x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\oddline1
  &\oct{03x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&\evenline
  &\oct{04x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\oddline2
  &\oct{05x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&\evenline
  &\oct{06x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\oddline3
  &\oct{07x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&\evenline
  &\oct{10x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\oddline4
  &\oct{11x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&\evenline
  &\oct{12x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\oddline5
  &\oct{13x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&\evenline
  &\oct{14x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\oddline6
  &\oct{15x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&\evenline
  &\oct{16x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\oddline7
  &\oct{17x}&&\:&&\:&&\:&&\:&&\:&&\:&&\:&&\:&\evenline}

% (now Appendix E resumes again)
% macros for verbatim scanning
\chardef\other=12
\def\ttverbatim{\begingroup
  \catcode`\\=\other
  \catcode`\{=\other
  \catcode`\}=\other
  \catcode`\$=\other
  \catcode`\&=\other
  \catcode`\#=\other
  \catcode`\%=\other
  \catcode`\~=\other
  \catcode`\_=\other
  \catcode`\^=\other
  \obeyspaces \obeylines \tt}

\outer\def\begintt{$$\let\par=\endgraf \ttverbatim \parskip=\z@
  \catcode`\|=0 \rightskip-5pc \ttfinish}
{\catcode`\|=0 |catcode`|\=\other % | is temporary escape character
  |obeylines % end of line is active
  |gdef|ttfinish#1^^M#2\endtt{#1|vbox{#2}|endgroup$$}}

\catcode`\|=\active
{\obeylines \gdef|{\ttverbatim \spaceskip\ttglue \let^^M=\  \let|=\endgroup}}

% macros for syntax rules (again, not in Appendix E)
\def\[#1]{\silenttrue\xref|#1|\thinspace{\tt#1}\thinspace} % keyword in syntax
\def\beginsyntax{\endgraf\nobreak\medskip
  \begingroup \catcode`<=13 \catcode`[=13
  \let\par=\endsyntaxline \obeylines}
\def\endsyntaxline{\futurelet\next\syntaxswitch}
\def\syntaxswitch{\ifx\next\<\let\next=\syntaxrule
  \else\ifx\next\endsyntax\let\next=\endgroup
  \else\let\next=\continuerule\fi\fi \next}
\def\continuerule{\hfil\break\indent\qquad}
\def\endsyntax{\medbreak\noindent}
{\catcode`<=13 \catcode`[=13
  \global\let<=\< \global\let[=\[
  \gdef\syntaxrule<#1>{\endgraf\indent\silentfalse\xref\<#1>}}
\def\is{\ $\longrightarrow$ }
\def\alt{\ $\vert$ }

% macros to demarcate lines quoted from TeX source files
\def\beginlines{\par\begingroup\nobreak\medskip\parindent\z@ \obeylines
  \hrule\kern1pt\nobreak \everypar{\strut}}
\def\endlines{\kern1pt\hrule\endgroup\medbreak\noindent}
\def\weakendlines{\kern1pt\hrule\endgroup\medskip\noindent}
\def\finalendlines{\kern1pt\hrule\endgroup\medbreak}

\outer\def\exercise{\medbreak
  \global\advance\exno by 1
  \noindent\llap{\manual\char'170\rm\kern.15em}% triangle in margin
  {\ninebf EXERCISE \bf\chapno.\the\exno}\par\nobreak\noindent}
\def\dexercise{\global\advance\exno by 1
  \llap{\manual\char'170\rm\kern.15em}% triangle in indented space
  {\eightbf EXERCISE \bf\chapno.\the\exno}\hfil\break}
\outer\def\dangerexercise{\d@nger \dexercise}
\outer\def\ddangerexercise{\dd@nger \dexercise}

\newwrite\ans
\immediate\openout\ans=answers % file for answers to exercises
\outer\def\answer{\par\medbreak
  \immediate\write\ans{}
  \immediate\write\ans{\string\ansno\chapno.\the\exno:}
  \copytoblankline}
\def\copytoblankline{\begingroup\setupcopy\copyans}
\def\setupcopy{\def\do##1{\catcode`##1=\other}\dospecials
  \catcode`\|=\other \obeylines}
{\obeylines \gdef\copyans#1
  {\def\next{#1}%
  \ifx\next\empty\let\next=\endgroup %
  \else\immediate\write\ans{\next} \let\next=\copyans\fi\next}}

% Editorial notes: some things to watch for.

% f |\ and f ^|\ => insert \/  [e.g., if\/ |\hbox|...]
% appendi => check for \null  [e.g., Appendix~B\null.]
% ly- => the hyphen is probably omittable
% ''. and '', => transpose to .'' and ,''
% dgement => dgment
% in MFbook: f@' and \MF, and \MF.

% Macros for drawing figures (not in Appendix E)
\def\hidehrule#1#2{\kern-#1\hrule height#1 depth#2 \kern-#2 }
\def\hidevrule#1#2{\kern-#1{\dimen0=#1
    \advance\dimen0 by#2\vrule width\dimen0}\kern-#2 }
% \makeblankbox puts rules at the edges of a blank box
% whose dimensions are those of \box0 (assuming nonnegative wd,ht,dp)
% #1 is rule thickness outside, #2 is rule thickness inside
\def\makeblankbox#1#2{\hbox{\lower\dp0\vbox{\hidehrule{#1}{#2}%
    \kern-#1% overlap the rules at the corners
    \hbox to\wd0{\hidevrule{#1}{#2}%
      \raise\ht0\vbox to #1{}% set the vrule height
      \lower\dp0\vtop to #1{}% set the vrule depth
      \hfil\hidevrule{#2}{#1}}%
    \kern-#1\hidehrule{#2}{#1}}}}
\def\maketypebox{\makeblankbox{0pt}{1pt}}
\def\makelightbox{\makeblankbox{.2pt}{.2pt}}

% \box\bigdot is a null box with a bullet at its reference point
\newbox\bigdot \newbox\smalldot
\setbox0=\hbox{$\vcenter{}$} % \ht0 is the axis height
\setbox1=\hbox to\z@{$\hss\bullet\hss$} % bullet is centered on the axis
\setbox\bigdot=\vbox to\z@{\kern-\ht1 \kern\ht0 \box1 \vss}
\setbox1=\hbox to\z@{$\hss\cdot\hss$} % cdot is centered on the axis
\setbox\smalldot=\vbox to\z@{\kern-\ht1 \kern\ht0 \box1 \vss}

% \arrows makes things like <--- text --->
\def\arrows#1#2{% #1=width, #2=text
  {\setbox0=\hbox{$\mkern-2mu\mathord-\mkern-2mu$}
    \hbox to #1{\kern-.055556em$\leftarrow\mkern-6mu$%
      \cleaders\copy0\hfil
      \kern.4em #2\kern.4em
      \cleaders\copy0\hfil
      $\mkern-6mu\rightarrow$\kern-.055556em}}}

% \samplebox makes the outline of a box, with big dot at reference point
\def\samplebox#1#2#3#4{% #1=ht, #2=dp, #3=wd, #4=text
  {\setbox0=\vtop{\vbox to #1{\hbox to #3{}\vss}
      \nointerlineskip
      \vbox to #2{}}% now \box0 has the desired ht, dp, and wd
    \hbox{\copy\bigdot
      \vrule height.2pt depth.2pt width#3%
      \kern-#3%
      \makelightbox
      \kern-#3%
      \raise#1\vbox{\hbox to #3{\hss#4\hss}
        \kern 3pt}}}}

% \sampleglue makes glue between sample boxes
\newdimen\varunit
\varunit=\hsize \advance\varunit by-2\parindent
\divide\varunit by 58 % illustrations in Chapter 12
\def\sampleglue#1#2{% #1=width, #2=text
  \vtop{\hbox to #1{\xleaders\hbox to .5\varunit{\hss\copy\smalldot\hss}\hfil}
    \kern3pt
    \tabskip \z@ plus 1fil
    \halign to #1{\hfil##\cr#2\cr}}}

% Indexing macros
\newif\ifproofmode
\proofmodetrue % this should be false when making camera-ready copy
\newwrite\inx
\immediate\openout\inx=index % file for index reminders
\newif\ifsilent
\def\specialhat{\ifmmode\def\next{^}\else\let\next=\beginxref\fi\next}
\def\beginxref{\futurelet\next\beginxrefswitch}
\def\beginxrefswitch{\ifx\next\specialhat\let\next=\silentxref
  \else\silentfalse\let\next=\xref\fi \next}
\catcode`\^=\active \let ^=\specialhat
\def\silentxref^{\silenttrue\xref}

\def\marginstyle{\vrule height6pt depth2pt width\z@ \sevenrm}

\chardef\bslash=`\\
\def\xref{\futurelet\next\xrefswitch}
\def\xrefswitch{\begingroup
  \ifx\next|\aftergroup\vxref % case 1 or 2, |arg| or |\arg|
  \else\ifx\next\<\aftergroup\anglexref % case 3, "\<arg>" means angle brackets
    \else\aftergroup\normalxref \fi\fi\endgroup} % case 0, "{arg}"
\def\vxref|{\catcode`\\=\active \futurelet\next\vxrefswitch}
\def\vxrefswitch#1|{\catcode`\\=0
  \ifx\next\empty\def\xreftype{2}%
    \def\next{{\tt\bslash\text}}% type 2, |\arg|
  \else\def\xreftype{1}\def\next{{\tt\text}}\fi % type 1, |arg|
  \edef\text{#1}\makexref}
{\catcode`\|=0 \catcode`\\=\active |gdef\{}}
\def\anglexref\<#1>{\def\xreftype{3}\def\text{#1}%
  \def\next{\<\text>}\makexref}
\def\normalxref#1{\def\xreftype{0}\def\text{#1}\let\next=\text\makexref}
\def\makexref{\ifproofmode\insert\margin{\hbox{\marginstyle\text}}%
   \xdef\writeit{\write\inx{\text\space!\xreftype\space
     \noexpand\number\pageno.}}\writeit
   \else\ifhmode\kern\z@\fi\fi
  \ifsilent\ignorespaces\else\next\fi}
% the \insert (which is done in proofmode only) suppresses hyphenation,
% so the \kern\z@ is put in to give the same effect in non-proofmode.

% Internal cross references that may change
\def\sesame{61} % page number for Sesame Street quote
\def\bmiexno{20} % exercise number for bold math italic
\def\punishexno{1} % exercise number for `punishment'
\def\fracexno{6} % exercise number for `\frac'
\def\vshippage{31} % error message from `\vship'
\def\storypage{24} % listing of story.tex
\def\metaT{4} % exercise number for T of METAFONT
\def\xwhat{2} % exercise number for x3:=whatever
\def\Xwhat{2} % exercise number for whatever itself

\def\checkequals#1#2{\ifnum#1=#2\else
  \errmessage{Redefine \string#1 to be \the#2}\fi}

% Things for The METAFONTbook only
\ifx\MFmanual\!\else\endinput\fi

\def\!{\kern-.03em\relax}

\def\frac#1/#2{\leavevmode\kern.1em
  \raise.5ex\hbox{\the\scriptfont0 #1}\kern-.1em
  /\kern-.15em\lower.25ex\hbox{\the\scriptfont0 #2}}

\outer\def\displayfig #1 (#2){$$\advance\abovedisplayskip by 3pt
  \leftline{\indent\figbox{#1}{3in}{#2}\vbox}$$}
\def\rightfig #1 (#2 x #3) ^#4 {% #2 wide and #3 deep, raised #4
  \strut\vadjust{\setbox0=\vbox to 0pt{\vss
      \hbox to\pagewidth{\hfil
        \raise #4\figbox{#1}{#2}{#3}\vtop \quad}}
    \dp0=0pt \box0}}
\def\figbox#1#2#3#4{#4to#3{ % makes a box #2 wide and #3 deep
    \ifproofmode\kern0pt\hrule\vfill
    \hsize=#2 \baselineskip 6pt \fiverm\noindent\raggedright
    (Figure #1 will be inserted here; too bad you can't see it now.)
    \endgraf\vfill\hrule
    \else\vfill\hbox to#2{}\fi}}

\def\endsyntax{\begingroup\let\par=\endgraf\medbreak\endgroup\noindent}

\let\BEGINCHAPTER=\beginchapter
\def\beginchapter{\titlelsl=1pt \BEGINCHAPTER}
\def\beginChapter{\titlelsl=2pt \BEGINCHAPTER}

\def\decreasehsize #1 {\advance\hsize-#1}
\def\restorehsize{\hsize=\pagewidth}

\catcode`\@=\active
\catcode`\"=\active
\def\ttverbatim{\begingroup \catcode`\@=\other \catcode`\"=\other
  \catcode`\\=\other
  \catcode`\{=\other
  \catcode`\}=\other
  \catcode`\$=\other
  \catcode`\&=\other
  \catcode`\#=\other
  \catcode`\%=\other
  \catcode`\~=\other
  \catcode`\_=\other
  \catcode`\^=\other
  \obeyspaces \obeylines \tt}
\def\setupcopy{\def\do##1{\catcode`##1=\other}\dospecials
  \catcode`\|=\other \catcode`\@=\other \catcode`\"=\other \obeylines}
\def\_{\leavevmode \kern.06em \vbox{\hrule width.3em}}
\def@#1@{\begingroup\def\_{\kern.04em
    \vbox{\hrule width.3em height .6pt}\kern.08em}%
  \ifmmode\mathop{\bf#1}\else\hbox{\bf#1\/}\fi\endgroup}
\def"#1"{\hbox{\it#1\/\kern.05em}} % italic type for identifiers
\def\xrefswitch{\begingroup
  \ifx\next|\aftergroup\vxref % case 1, |arg| or |\arg|
  \else\ifx\next@\aftergroup\boldxref % case 2, "@arg@" means boldface
  \else\ifx\next"\aftergroup\italxref % case 4, ""arg"" means boldface
  \else\ifx\next\<\aftergroup\anglexref % case 3, "\<arg>" means angle brackets
    \else\aftergroup\normalxref \fi\fi\fi\fi\endgroup} % case 0, "{arg}"
\def\boldxref@#1@{\def\xreftype{2}\def\text{#1}%
  \def\next{@\text@}\makexref}
\def\italxref"#1"{\def\xreftype{4}\def\text{#1}%
  \def\next{"\text"}\makexref}

\def\pyth+{\mathbin{++}}
\def\0{\raise.7ex\hbox{$\scriptstyle\#$}}
\def\to{\mathrel{\ldotp\ldotp}}
\def\dashto{\mathrel{\hbox{-\thinspace-\kern-.05em}}}
\def\ddashto{\mathrel{\hbox{-\thinspace-\thinspace-\kern-.05em}}}
\def\round{\mathop{\rm round}}
\def\angle{\mathop{\rm angle}}
\def\rmsqrt{\mathop{\rm sqrt}}
\def\reverse{\mathop{\rm reverse}}
\def\curl{\mathop{\rm curl}}
\def\tension{\mathop{\rm tension}}
\def\atleast{\mathop{\rm atleast}}
\def\controls{\mathop{\rm controls}}
\def\and{\,{\rm and}\,}
\def\cycle{{\rm cycle}}
\def\pickup{@pickup@ \thinspace}
\def\penpos#1{\hbox{\it penpos}_{#1}}
\def\pentaper#1{\hbox{\it pentaper}_{#1}}

\chardef\hexa=1  % first hex
\chardef\hexb=2 % top and bot adjusted
\chardef\hexc=3 % same, bold
\chardef\hexd=4 % same, confined to box
\chardef\hexe=5 % penstroked hex
\chardef\Aa=6 % stick-figure A, golden ratio
\def\sevenAs{\char7\char8\char9\char10\char11\char12\char13} % same, variants
\chardef\Az=14 % same, with crooked bar
\chardef\Ab=15 % \Aa with rectilinear elliptical pen
\chardef\Ac=16 % same, with the ellipse tilted
\chardef\beana=17 % kidney bean, default pen
\chardef\beanb=18 % same, twice as bold
\chardef\beanc=19 % same, rectilinear elliptical pen
\chardef\beand=20 % same, with the ellipse tilted
\chardef\niba=21 % 10x rectilinear ellipse
\chardef\nibb=22 % same, with the ellipse tilted
\chardef\nibc=23 % same, 90 degrees titled
\chardef\IOT=24 % Ionian T
\chardef\IOS=25 % Ionian S
\chardef\IOO=26 % Ionian O
\chardef\IOI=27 % Ionian I
\chardef\cubea=28 % possible cube
\chardef\cubeb=29 % impossible cube
\chardef\bicentennial=30 % star with overlapping strokes
\chardef\oneu=31 % 1/4 of uuuu ornament
\chardef\circa=32 % quartercircle
\chardef\circb=33 % filled quartercircle
\chardef\circc=34 % rotated quartercircle
\chardef\circd=35 % cone
\chardef\circe=36 % concentric circles
\chardef\circf=37 % concentric diamonds
\chardef\fouru=38 % uuuu ornament
\chardef\fourc=39 % same, rotated
\chardef\seventh='140 % 1/7, to go with cmssqi8

\newdimen\apspix
\apspix=31448sp % 8 APS pixels = 52413.64sp, and I'm taking 60% of this
% to crude approximation, there are about 2\apspix per pt
\newdimen\blankpix \newdimen\Blankpix
\setbox0=\hbox{\manual P} \blankpix=\wd0 % approximately 1pt blank pixel
\setbox0=\hbox{\manual R} \Blankpix=\wd0 % approximately 3pt blank pixel

\def\leftheadline{\hbox to \pagewidth{%
    \vbox to 10pt{}% strut to position the baseline
    \llap{\tenbf\folio\kern1pc}% folio to left of text
    \def\MF{{\manual 89:;<=>:}}% slanted 10pt
    \tenit\rhead\hfil% running head flush left
    }}
\def\rightheadline{\hbox to \pagewidth{%
    \vbox to 10pt{}% strut to position the baseline
    \def\MF{{\manual 89:;<=>:}}% slanted 10pt
    \hfil\tenit\rhead\/% running head flush right
    \rlap{\kern1pc\tenbf\folio}% folio to right of text
    }}
\def\ttok#1{\leavevmode\thinspace\hbox{\vrule\vtop{\vbox{\hrule\kern1pt
        \hbox{\vphantom{\tt(j}\thinspace{\tt#1}\thinspace}}
      \kern1pt\hrule}\vrule}\thinspace} % token

\newdimen\tinypix \setbox0=\hbox{\sixrm0} \tinypix=5pt
\newdimen\pixcorr \pixcorr=\tinypix \advance\pixcorr by-\wd0
\def\pixpat#1#2#3#4{\vcenter{\sixrm\baselineskip=\tinypix
  \hbox{#1\kern\pixcorr#2}\hbox{#3\kern\pixcorr#4}}}

\font\rand=random
